package kjh820909.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JRadioButton;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.Dimension;

import javax.swing.JLabel;

import java.awt.CardLayout;

import javax.swing.ImageIcon;

import java.awt.Color;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextPane;
import javax.swing.JTabbedPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtUid;
	private JTextField txtPwd;
	private JTextField textField;
	private final CardLayout cards =
			new CardLayout(0,0); // 1
	private JPanel myPane = new JPanel();
	private JTabbedPane mainPane = new JTabbedPane(JTabbedPane.TOP);

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AComp dialog = new AComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AComp() {
		setTitle("고급 컴퍼넌트");
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel top = new JPanel();
			top.setPreferredSize(new Dimension(10, 120));
			contentPanel.add(top, BorderLayout.NORTH);
			top.setLayout(new BorderLayout(0, 0));
			{
				JLabel lblNewLabel = new JLabel("New label");
				lblNewLabel.setPreferredSize(new Dimension(400, 15));
				lblNewLabel.setIcon(new ImageIcon(AComp.class.getResource("/kjh820909/swing/images/background/top.jpg")));
				top.add(lblNewLabel, BorderLayout.WEST);
			}
			{
				final JPanel cardPane = new JPanel();
				cardPane.setPreferredSize(new Dimension(210, 10));
				top.add(cardPane, BorderLayout.EAST);
				cardPane.setLayout(cards);
				{
					JPanel loginPane = new JPanel();
					loginPane.setBackground(Color.PINK);
					cardPane.add(loginPane, "login");
					
					txtUid = new JTextField();
					txtUid.setColumns(10);
					
					txtPwd = new JTextField();
					txtPwd.setColumns(10);
					
					JButton loginBtn = new JButton("로 그 인");
					loginBtn.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
						
						String uid = txtUid.getText();
						String pwd = txtPwd.getText();
						
						if(uid.equals("abc") && pwd.equals("12345")){
							cards.show(cardPane,"logout");
							// myPane.setEnabled(false);
							mainPane.setEnabledAt(2,true);
						}else 
							JOptionPane.showMessageDialog(null, "틀리다");
						}
						});
					// cards : 카드레이아웃
					// cardPane : 카드레이아웃이 적용된 패널
					// logout : cardPane에 보여줄 패널 이름
					
					JButton joinBtn = new JButton("회원가입");
					GroupLayout gl_loginPane = new GroupLayout(loginPane);
					gl_loginPane.setHorizontalGroup(
						gl_loginPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_loginPane.createSequentialGroup()
								.addContainerGap()
								.addGroup(gl_loginPane.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(txtPwd, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
									.addComponent(txtUid, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
								.addPreferredGap(ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
								.addGroup(gl_loginPane.createParallelGroup(Alignment.LEADING)
									.addComponent(joinBtn)
									.addComponent(loginBtn))
								.addContainerGap())
					);
					gl_loginPane.setVerticalGroup(
						gl_loginPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_loginPane.createSequentialGroup()
								.addGap(39)
								.addGroup(gl_loginPane.createParallelGroup(Alignment.BASELINE)
									.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
									.addComponent(loginBtn))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(gl_loginPane.createParallelGroup(Alignment.BASELINE)
									.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(joinBtn))
								.addContainerGap(25, Short.MAX_VALUE))
					);
					loginPane.setLayout(gl_loginPane);
				}
				{
					JPanel logoutPane = new JPanel();
					logoutPane.setBackground(Color.YELLOW);
					cardPane.add(logoutPane, "logout");
					
					JLabel myAvatar = new JLabel("");
					myAvatar.setIcon(new ImageIcon(AComp.class.getResource("/kjh820909/swing/images/icon/icon2.png")));
					
					JLabel myNick = new JLabel("천송이");
					
					JButton logoutBtn = new JButton("로그아웃");
					logoutBtn.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							cards.show(cardPane,"login");
							mainPane.setEnabledAt(2, false);
						}
					});
					GroupLayout gl_logoutPane = new GroupLayout(logoutPane);
					gl_logoutPane.setHorizontalGroup(
						gl_logoutPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_logoutPane.createSequentialGroup()
								.addContainerGap()
								.addComponent(myAvatar)
								.addGroup(gl_logoutPane.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addGap(45)
										.addComponent(myNick))
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addGap(27)
										.addComponent(logoutBtn)))
								.addContainerGap(42, Short.MAX_VALUE))
					);
					gl_logoutPane.setVerticalGroup(
						gl_logoutPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_logoutPane.createSequentialGroup()
								.addGap(26)
								.addGroup(gl_logoutPane.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addComponent(myNick)
										.addGap(18)
										.addComponent(logoutBtn))
									.addComponent(myAvatar))
								.addContainerGap(30, Short.MAX_VALUE))
					);
					logoutPane.setLayout(gl_logoutPane);
				}
			}
		}
		
		
		contentPanel.add(mainPane, BorderLayout.CENTER);
		
		JPanel basicFind = new JPanel();
		mainPane.addTab("기본검색", new ImageIcon(AComp.class.getResource("/kjh820909/swing/images/menu/close.png")), basicFind, null);
		
		JButton btnNewButton = new JButton("New button");
		basicFind.add(btnNewButton);
		
		JPanel detailFind = new JPanel();
		mainPane.addTab("상세검색", new ImageIcon(AComp.class.getResource("/kjh820909/swing/images/menu/exit.png")), detailFind, null);
		
		textField = new JTextField();
		detailFind.add(textField);
		textField.setColumns(10);
		
		mainPane.addTab("마이페이지", new ImageIcon(AComp.class.getResource("/kjh820909/swing/images/menu/new.png")), myPane, null);
		mainPane.setEnabledAt(2, false);
		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
		myPane.add(rdbtnNewRadioButton);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			
		}
	}
}
